/**
 * Author Rickard Bremer 810205-4890
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;

public class search {

	// public static String richard;
	public static String arneAnka = "";
	public static String driver = "com.mysql.jdbc.Driver";
	public static String userName = "root";
	public static String password = "";
	public static String url = "jdbc:mysql://localhost:3306/";
	public static String dbName = "lindholmen";
	public static String sqlBit = "";

	static ArrayList<String> resultat = new ArrayList<String>();

	/* Recives following information from GUI , the user */
	public static void find(String typeOfFood, String typeOfRestaurant,
			String price) {

		System.out.println(price); /* testing */

		/* Code for searching in price intervall */

		if (price.equals("50-100kr")) {
			sqlBit = "BETWEEN 0 AND 1000";
		}
		if (price.equals("101-150kr")) {
			sqlBit = "BETWEEN 101 AND 150";
		}
		if (price.equals("151-200kr")) {
			sqlBit = "BETWEEN 151 AND 200";
		}
		if (price.equals("201kr")) {
			sqlBit = " ";
		}

		System.out.print(typeOfFood + typeOfRestaurant + price);
		System.out.println("Search Database.");

		/* Search query without price for now */

		String sql = "SELECT * FROM food  WHERE FType='" + typeOfRestaurant
				+ "' AND RType='" + typeOfFood + "'";

		Connection con = null;

		try {

			Class.forName(driver).newInstance();
			con = DriverManager.getConnection(url + dbName, userName, password);
			System.out.println("Connected to the database");
			System.out.println("------------");
			Statement stmt = (Statement) con.createStatement();

			ResultSet rs = stmt.executeQuery(sql);

			while (rs.next()) {
				System.out.println("Price : " + rs.getString("FPrice"));
				System.out.println("Food      : " + rs.getString("Ftype"));
				System.out
						.println("Dish Name       : " + rs.getString("FName"));
				System.out.println("------------");
				arneAnka += "Price : " + rs.getString("FPrice") + "\n "
						+ "Type : " + rs.getString("FType") + "\n "
						+ "Restaurant :" + rs.getString("FName") + "\n";

				System.out.println(arneAnka); /*
											   * putting search result in
											   * arraylist
											   */
				resultat.add(arneAnka);

			}

			con.close();

			for (int i = 0; i < resultat.size(); i++) {
				guiReworked.searchresult(resultat.get(i));  /*
															 * Create object
															 * with search
															 * result
															 */
			}

			System.out.println("Disconnected from database");
			System.out.print(typeOfFood + " " + typeOfRestaurant);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
